package com.report.service;

import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.URLUtil;
import com.report.enums.FileEnum;
import com.report.file.PdfFile;
import com.report.pdf.PdfUtils;
import com.report.utils.ZipUtils;
import com.zmh.framework.constants.Constants;
import com.zmh.framework.exception.ResultException;
import com.zmh.framework.oss.OssConfigure;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.List;

/**
 * @author summer
 * @date 2024-06-22 11:32
 */
@Service
@AllArgsConstructor
@Slf4j
public class UploadFileService {

    private final ZipUtils zipUtils;
    
    private final OssConfigure ossConfigure;

    /**
     * 上传多个文件
     * @param files
     * @return
     */
    public List<PdfFile> uploadZipFile(MultipartFile[] files) {
        List<PdfFile> paths= ListUtil.list(true);
        List<PdfFile> unzipList = zipUtils.unzip(files);
        unzipList.forEach(unzip->{
            PdfFile path=new PdfFile();
            path.setFilePath(ossConfigure.getAccessUrl()+ossConfigure.uploadFile(unzip.getFile()));
            List<PdfFile> lists= ListUtil.list(true);
            unzip.getFileList().forEach(file1->{
                PdfFile pa=new PdfFile();
                pa.setFilePath(ossConfigure.getAccessUrl()+ossConfigure.uploadFile(file1.getFile()));
                lists.add(pa);
            });
            path.setFileList(lists);
            paths.add(path);
        });

        return paths;
    }



    public List<PdfFile> uploadPDFList(MultipartFile[] files) {
            List<PdfFile> pdfFileList=ListUtil.list(true);

            for (MultipartFile file : files) {
                PdfFile pdfFile=new PdfFile();
                String afterName = FileUtil.extName(file.getOriginalFilename());
                assert afterName != null;
                if (!afterName.toLowerCase().equals(FileEnum.PDF.name().toLowerCase())) {
                    throw new ResultException(file.getOriginalFilename()+"文件格式不对");
                }
                if(file.getOriginalFilename().contains("+")){
                    throw new ResultException(file.getOriginalFilename()+"文件格式不能带+");
                }
                try {
                    String fileName = ossConfigure.uploadFile(file);
                    pdfFile.setFilePath(ossConfigure.getAccessUrl()+fileName);
                    pdfFile.setFileName(file.getOriginalFilename());
                    Integer pages = PdfUtils.getPages(file.getInputStream());
                    String image = PdfUtils.getImage(file.getInputStream(), ossConfigure.getTempFile());
                    String s = uploadOss(image);
                    pdfFile.setImagePath(s);
                    pdfFile.setPages(pages);
                    pdfFileList.add(pdfFile);
                }catch (Exception e){
                    throw new ResultException(e.getMessage());
                }

            }
            return pdfFileList;


    }
    public String uploadOss(String image){


            String imagePath = ossConfigure.getAccessUrl()+ossConfigure.uploadFile(FileUtil.file(image));
            FileUtil.del(image);

        return imagePath;
    }



}
